package com.usian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.feign.ArticleFeign;
import com.usian.feign.WmUserFeign;
import com.usian.mapper.ApUserMapper;
import com.usian.mapper.ApUserRealnameMapper;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.model.user.dtos.AuthDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.service.ApUserRealnameService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {


    @Autowired
    private WmUserFeign wmUserFeign;
    @Autowired
    private ArticleFeign articleFeign;

    @Autowired
    private ApUserMapper apUserMapper;


    @Override
    public ResponseResult findApUserRealname(AuthDto dto) {

        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        //分页查询
        Page pageparam =new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ApUserRealname> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        if(dto.getStatus()!=null){
            lambdaQueryWrapper.eq(ApUserRealname::getStatus,dto.getStatus());
        }
        IPage result =this.page(pageparam,lambdaQueryWrapper);
        ResponseResult responseResult =new PageResponseResult(dto.getPage(),dto.getSize(),(int)result.getTotal());
        responseResult.setData(result.getRecords());

        return responseResult;
    }

    @Override
    @GlobalTransactional
    public ResponseResult updateStatus(AuthDto dto) {

        /**
         * 1: ap_user_realname  status  9通过 2未通过
         * 2：ap_user_realname userId  ap_user查询对象
         * 3：wm_user  表 存在则不保存 不存在则保存
         * 4：ap_author wm_user  userid    在则不保存 不存在则保存
         * 5：ap_user  update  flag
         *
         */

        //1:校验参数
        if(dto==null || dto.getId()==null|| dto.getStatus()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2:跟新apuserealname表 status状态   9 通过   2不通过
        ApUserRealname apUserRealname =new ApUserRealname();
        apUserRealname.setId(dto.getId());
        apUserRealname.setStatus(dto.getStatus());
        if(dto.getStatus()==2){
            //不通过  维护不通过原因
            apUserRealname.setReason(dto.getMsg());
        }
        this.updateById(apUserRealname);

        if(dto.getStatus()==9){
            //3:dto id值查询apuser对象，根据apuser id 去apuserrealname表根据名字查询  校验该用户是否注册过自媒体账号
            ApUserRealname userRealname = this.getById(dto.getId());
            if(userRealname==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }

            ApUser apUser = apUserMapper.selectById(userRealname.getUserId());
            if(apUser==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
            }

            WmUser wmUser = wmUserFeign.findByName(apUser.getName());
            if(wmUser==null){
                //4:无账号 创建新账号 远程调用插入一条数据
                wmUser =new WmUser();
                wmUser.setApUserId(apUser.getId());
                wmUser.setCreatedTime(new Date());
                wmUser.setSalt(apUser.getSalt());
                wmUser.setName(apUser.getName());
                wmUser.setPassword(apUser.getPassword());
                wmUser.setStatus(9);
                wmUser.setPhone(apUser.getPhone());
                wmUserFeign.save(wmUser);

            }

            //5: 用wmuser表中 apuserid 去 ap_author表中查询是否存在作者
            ApAuthor apAuthor = articleFeign.findByUserId(wmUser.getApUserId());
            if(apAuthor==null){
                //6: 不存在 远程调用保存
                apAuthor =new ApAuthor();
                apAuthor.setName(wmUser.getName());
                apAuthor.setType(2);
                apAuthor.setCreatedTime(new Date());
                apAuthor.setUserId(wmUser.getApUserId());

                //查询  wmuser 根据 用户名 查询 wmuser表主键id
                WmUser byName = wmUserFeign.findByName(apUser.getName());
                if(byName!=null){
                    apAuthor.setWmUserId(byName.getId());
                }
                articleFeign.saveAuthor(apAuthor);
            }

            //6：修改apuser表中flag为1  自媒体人
            apUser.setFlag((short) 1);
            apUserMapper.updateById(apUser);

        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
